package com.liuche.homechat.common.common.config;

import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadFactory;

/**
 * @author：liuche
 * @Date：2024年06月01日 16:20
 * @Description：使用装饰器模式在创建线程时增加异常捕获
 */
@AllArgsConstructor
@NoArgsConstructor
@Slf4j
public class MyThreadFactory implements ThreadFactory {
    private ThreadFactory originalFactory;
    @Override
    public Thread newThread(Runnable r) {
        Thread thread = originalFactory.newThread(r);
        thread.setUncaughtExceptionHandler((t, e) -> log.error("Thread->【{}】；Exception-> 【{}】", t,e.getMessage()));
        return thread;
    }
}
